<script type="text/javascript" src="/js/merchant/warehouseproducts.js"></script>
<script type="text/javascript">
jQuery.extend(jQuery.validator.messages, {
	required: "<img src='/images/error.jpg'>This field is required.",
	remote:"<img src='/images/error.jpg'>This reference No has exists"
});
$(function(){

	$("#tipDialog").dialog({
		autoOpen:false,
		modal:true,
		width:300,
		show:"slide",
		buttons:{
			"Close":function(){
				$(this).dialog("close");				
			}
		}
	});
	
	$("[name=toChange]:eq(0)").click(function(){
		$(".by_order").show();
		$(".by_product").hide();
		$(".all_weight").html('');
		$("[name=order_type]").val(2);
		clear();
	});
	
	$("[name=toChange]:eq(1)").click(function(){
		$(".by_product").show();
		$(".by_order").hide();
		$(".all_weight").html('');
		$("[name=order_type]").val(3);
		$('#product_return').empty().hide();
		$('[name=reference_no]').attr("disabled", false);
		clear();
	});
	
	$("#editForm").validate({
		rules:{
			'reference_no':{
				required: true,
				remote:{
					url:"/merchant/receiving/check-refno",
					type: "post",               //数据发送方式
  					dataType: "json",           //接受数据格式  
					data:{
						reference_no:function() {return $('[name=reference_no]').val();}
					}
				}
			}
		},
		errorPlacement:function(error, element){
			element.parent().parent().children(".right").html(error);  
		},
		success:function(label){
			label.parent().parent().children(".right").html("<img src='/images/yes.gif'>");
		}
	});
	
});

function checkProduct(){
	 if($(".product_id").length==0){
		$(".productError").html("<img src='/images/error.jpg'>Product can not be empty!");
		$(".productError").show();
		return false;
	 }
	 $(".productError").html("");
	 $(".productError").hide();
	 return true;
}

function sumbitForm(){
    var formData =$('#editForm').serialize();
    if(!$("#editForm").valid() | !checkProduct()){
		return false;
	}
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/returns/create",
		data:formData,
		success: function(json) {
			var html ="";
            if(json.ask=='Success'){
                $("#tipDialog").html('RMA No: '+json.receiving_code+' Add success!');                
            }else{               
				$("#tipDialog").html(' Add failed! '+json.msg);
            }
            clear();
            $("#tipDialog").dialog("open");
		}
	});
}

function submitCode(){
	$('#product_return').empty();
	var header = '';
	header += '<div class="row height25 row-header">';
	header += '<div class="cell sort" style="width:20%" >SKU</div>';
	header += '<div class="cell sort" style="width:25%" >SKU Name</div>';
	header += '<div class="cell sort" style="width:20%" >Category</div>';
	header += '<div class="cell sort" style="width:15%" >Quantity</div>';
	header += '<div class="cell sort" style="width:10%" >Weight</div>';
	header += '<div class="cell no-background" style="width:10%">Action</div>';
	header += '</div>';
	$("#product_return").append(header).show();
	
	var orders_code = $('[name=orders_code]').val();	
	$('[name=reference_no]').val(orders_code);
	$('[name=reference_no]').attr("disabled", "disabled");
	$('[name=orders_code_2]').val(orders_code);
	
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/returns/get-products",
		data:{ 'orders_code':orders_code },
		success: function(json) {
			var html = '';
			if(json.ask != 1) {
				html = '<div class="line1" style="color:red;">'+json.msg+'</div>';
			}else {							
				$.each(json.result,function(k,v){
					html += '<div class="row height25 productRow'+v.product_id+'">';
					html += '<div class="cell" style="width:20%">'+v.product_sku+'</div>';
					html += '<div class="cell" style="width:25%">'+v.product_title+'</div>';
					html += '<div class="cell" style="width:20%">'+v.category.poc_value+'</div>';
					html += '<div class="cell" style="width:15%"><input type="text" class="inputText" name="op_quantity[]" value="'+v.order_qty+'" style="width:80px;text-align:right" id="op_quantity'+v.product_id+'"  onblur="calWeihgt()"/></div>';
					html += '<div class="cell" style="width:10%" id="product_weight'+v.product_id+'">'+v.product_weight+'</div>';
					html +='<div class="cell" style="width:10%;"><img src="/images/icon_del.gif" name="button'+v.product_sku+'" onclick="deleteProduct(this)" class="cursor" title="Delete"/><input type="hidden"  name="product_id[]" class ="product_id" value="'+v.product_id+'" /><input type="hidden"  name="op_id[]" value="0" /></div>';
					html += '<div class="clearFloat"></div>';
					html += '</div>';
				});	
				html += '<div class="row height25">';
				html += '<div class="cell" style="width:80%;text-align:right" >Total Weight &nbsp; </div>';
				html += '<div class="cell all_weight"  style="width:20%;text-align:left" ></div>';
				html += '</div>';
			}
			
			$("#product_return").append(html);	
			$(".productError").html('');
			calWeihgt();		
		}	//success
	});
	
}

function clear(){
	$('[name=reference_no]').val('');
	$('[name=instructions]').val('');
	$('#ProductList').html('');
	$('#product_return').html('');
	$('.right').html('');
	$('.all_weight').html('');
}
</script>
<style type="text/css">
div.selectDiv{
	margin-top:8px;margin-bottom:15px;
}


</style>
<div class="position"><a href='/default/index'>Home</a> &gt; <a href='/merchant/my-account'>Merchant Center</a>  &gt; Operation &gt; Return </div>
<h4>Return Request</h4>
<div class="selectDiv">
	<div>
		<span style="font-size:14px;font-weight:bold">The Way Of Return</span><br/>
		<p>
		<input type="radio" name="toChange" checked /> According to Order No. &nbsp;&nbsp;&nbsp;&nbsp; 
		<input type="radio" name="toChange" />	Choose Products
		</p>
	</div>
</div>

<form id="editForm"  method="POST" >
<input type="hidden" name="order_type" value="2" />	<!-- 退货申请单类型-->
<input type="hidden" name="orders_code_2" value="" />	<!--  订单号 -->
<div class="by_order" style="margin-bottom:10px;">
		Order No.: <input type="text" name="orders_code" value="" class="" />	&nbsp;  		
		<span class="button-2" onclick="submitCode()" id="btnCode">Submit</span>
</div>

<div class="tab-div" id="product_return" style="display:none">

</div>

<div class="tab-div by_product" style="display:none; line-height:12px;">

	<div class="row">
		<div class="right"  style="width:20%;padding-left: 5px;font-weight:bold">Reciving List:</div>
		<div class="middle">			
			<span class="button-2"  onclick="OpenProductDiv()">Add Product</span>
			<span class="red">&nbsp;&nbsp;</span>
		</div>
		<div class="right"></div>
	</div>
	
	<div class="row" id="OrderProduct">
		<div class="row height25 row-header">
			<div class="cell sort" style="width:18%" >SKU</div>
			<div class="cell sort" style="width:18%" >SKU Name</div>
            <div class="cell sort" style="width:15%" >Chinese Name</div>
			<div class="cell sort" style="width:18%" >Category</div>
			<div class="cell sort" style="width:13%" >Quantity</div>
			<div class="cell sort" style="width:10%" >Weight</div>
			<div class="cell no-background" style="width:8%">Action</div>
		</div>
		<div id="ProductList" ></div>
	</div>
	
	<div class="row height25">
		<div class="cell" style="width:82%;text-align:right" >Total Weight &nbsp;  </div>
		<div class="cell all_weight"  style="width:18%;text-align:left" ></div>
	</div>

</div>
<div class="tab-div">
	<div class="productError row height25" style="display:none"></div>
</div>

<h4>Additional Info</h4>
<div class="tab-div">
    
	<div class="row" style="line-height:16px;">
		<div class="left"  style="width:20%; line-height:25px;">Reference No:&nbsp;&nbsp;</div>
    	<div class="middle">
    		<input type="text" name="reference_no" value="" style="width:235px;" class="inputText remote" />
    		<span class="red">&nbsp;&nbsp;*</span>
    	</div>
    	<div class="right"></div>
	</div>
	
	<div class="row" style="padding-left:5px;padding-top:5px">
		<div class="left"  style="width:20%;height:95px;">Special Instructions:&nbsp;&nbsp;</div>
    	<div class="middle">
    		<textarea cols="38" rows="5" name="instructions" ></textarea>
			<input type="hidden" name="receiving_id" value="" />	<!-- receiving_id，新建时 为空-->
    		<span class="red">&nbsp;&nbsp;</span>
    	</div>
    	<div class="right"></div>
	</div>
    
    <div class="row" style="height:40px;align:center">

    	<div style="margin-top:10px;margin-left:150px">    		
    		<span class="button-2" onclick="sumbitForm()">Create Apply</span>    	
    	</div>
    </div>
</div>
</form>


<div class="prdouctDiv" style="display:none" title="Product List">
<{include file='merchant/orders/warehouse-products-list.tpl'}>
</div>
	
<div style="display:none" id="tipDialog" title="Tip"> </div>